package com.example.service.xitongshouye.impl;

import com.example.dao.xitongshouye.Xtshouye;
import com.example.pojo.Course;
import com.example.pojo.CourseRecord;
import com.example.pojo.Product;
import com.example.pojo.User;
import com.example.service.xitongshouye.XtshouyeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

@Service
public class XtshouyeServiceImpl implements XtshouyeService {
    @Autowired
    private Xtshouye xtshouye;


    /**
    * @Author: zdp
    * @Description: 计算总量
    * @DateTime: 13:30 2022/11/13
    * @Params:
    * @Return
    */
    public Map<String,Integer> all(){
        List<Course> all = xtshouye.all();
        Map<String,Integer> map = new HashMap<>();
        int a = 0;//上传视频总量
        int b = 0;//上传视频浏览数
        int c = 0;//收藏量
        int d = 0;//点赞量
        //循环计算总量
        for (Course course : all) {
            a++;
            if(course.getLookNum()!=null){
                b+=course.getLookNum();
            }
            if(course.getCollectNum()!=null){
                c+=course.getCollectNum();
            }
            if(course.getLikeNum()!=null){
                d+=course.getLikeNum();
            }
        }
        map.put("count",a);
        map.put("looknum",b);
        map.put("collectnum",c);
        map.put("likenum",d);
        return map;
    }
    /**
    * @Author: zdp
    * @Description: 课程总数量
    * @DateTime: 13:31 2022/11/13
    * @Params:
    * @Return
    */
    public int kcount(){
        List<Course> all = xtshouye.all();
        int a=0;
        for (Course course : all) {
            a++;
        }
        return a;
    }
    /**
    * @Author: zdp
    * @Description: 商品总数量
    * @DateTime: 13:39 2022/11/13
    * @Params:
    * @Return
    */
     public int scount(){
         List<Product> sall = xtshouye.sall();
         int a = 0;
         for (Product product : sall) {
             a++;
         }
         return a;
     }

    /**
    * @Author: zdp
    * @Description: 用户总览
    * @DateTime: 9:59 2022/11/15
    * @Params: 
    * @Return 
    */
    public List<Integer> userall() {
        List<User> uall = xtshouye.uall();//用户全览表//当前时间
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dangqian = LocalDateTime.parse(dateFormat.format(new Date()), formatter1);//当前时间格式

        List<Integer> d1 = new ArrayList<>();
        int a=0;//总用户
        int b=0;//当月新增用户
        int c=0;//当天新增用户
        int d=0;//昨天新增用户
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.0");//规定日期格式
        for (User user : uall) {
            a++;
            if(user.getRegisterDate()!=null){
                LocalDateTime ldt = LocalDateTime.parse(user.getRegisterDate() + "", formatter);
                if(dangqian.getYear()==ldt.getYear()&&dangqian.getMonth().getValue()==ldt.getMonth().getValue()){
                    b++;
                    if(dangqian.getDayOfMonth()==ldt.getDayOfMonth()){
                        c++;
                    }else if(ldt.getDayOfMonth()==dangqian.getDayOfMonth()-1||(ldt.getDayOfMonth()==31)==(dangqian.getDayOfMonth()-1==0)){
                        d++;
                    }
                }
            }
        }
        d1.add(c);
        d1.add(d);
        d1.add(b);
        d1.add(a);
        return d1;
    }

    /**
    * @Author: zdp
    * @Description: 待处理事务
    * @DateTime: 16:34 2022/11/23
    * @Params:
    * @Return
    */
    public List shenhe(){
        List shen = new ArrayList();
        List<CourseRecord> shenhe = xtshouye.shenhe();
        int a = 0;//待处理视频
        int b = 0;//待处理商品
        for (CourseRecord courseRecord : shenhe) {
            if(courseRecord.getType()==2){
                a++;
            }else{
                b++;
            }
        }
        shen.add(a);
        shen.add(b);
        return shen;
    }
    /**
     * @Author: zdp
     * @Description: 查询某时间段内的课程
     * @DateTime: 9:58 2022/11/15
     * @Params:
     * @Return
     */
    public List call(String s1, String s2){
      List f1 = new ArrayList();
        try {
            SimpleDateFormat sdf1 =   new SimpleDateFormat( "yyyy-MM-dd" );
            //开始时间
            Date startTime= sdf1.parse(s1);
            //结束时间
            Date endTime= sdf1.parse(s2);
            Date tmp = startTime;
            Calendar dd = Calendar.getInstance();
            dd.setTime(startTime);

            List da = new ArrayList<>();
            while(tmp.getTime()<= endTime.getTime()){
                tmp =dd.getTime();
                String fee = sdf1.format(tmp);
                dd.add(Calendar.DAY_OF_MONTH,1);//天数加一天
                da.add(fee);//添加到集合里
            }
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            List<Course> call = xtshouye.call(s1,s2);
            List<Integer> ii = new ArrayList<>();
            int[] iii = new int[da.size()];

            for (int i=0;i<da.size();i++){
                for (int j = 0; j < call.size(); j++) {
                    Timestamp createDate = call.get(j).getCreateDate();
                    String d1 = sdf.format(createDate);
                    if(da.get(i).equals(d1)){
                        iii[i]+=1;
                        //ii.add(i,+1);
                    }
                }
            }
            f1.add(da);
            f1.add(iii);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return f1;
    }

    /**
    * @Author: zdp
    * @Description: 课程总数同比上月增长百分比
    * @DateTime: 15:01 2022/11/22
    * @Params:
    * @Return
    */
    public List keall() {
        List s1 = new ArrayList();

        DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate date1 = LocalDate.now();
        String str = sdf.format(date1);//当前日期
        LocalDate firstDay = date1.with(TemporalAdjusters.firstDayOfMonth());
        String str1 = sdf.format(firstDay);//本月第一天

        LocalDateTime date = LocalDateTime.now().minusMonths(1);
        LocalDateTime firstday = date.with(TemporalAdjusters.firstDayOfMonth());
        LocalDateTime lastDay = date.with(TemporalAdjusters.lastDayOfMonth());
        String format = firstday.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));//上个月第一天
        String format1 = lastDay.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));//上个月最后一天

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        String ss = sdf.format( cal.getTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()) ;//本周第一天


        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.add(Calendar.DATE, -1 * 7);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        String ss1 = sdf.format( cal.getTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()) ;//上周第一天


        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        String ss2 = sdf.format(cal.getTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate());//上周最后一天

        List<Course> call = xtshouye.call(ss,str);//本周申请成功
        int benzhou=0;
        for (Course course : call) {
            if(course.getCourseCstatus()==6){
                benzhou++;
            }
        }
        List<Course> call1 = xtshouye.call(ss1, ss2);//上周申请成功
        int shangzhou=0;
        for (Course course : call1) {
            if(course.getCourseCstatus()==6){
                shangzhou++;
            }
        }
        int ben = xtshouye.keChengZong(str1, str);
        int shang = xtshouye.keChengZong(format, format1);
        s1.add(ben);
        s1.add(shang);
        s1.add(benzhou);
        s1.add(shangzhou);
        return s1;
    }


}
